package cn.sangyy.car.controller;

import cn.sangyy.basic.utils.LoginContext;
import cn.sangyy.car.query.CarQuery;
import cn.sangyy.car.service.ICarService;
import cn.sangyy.car.domain.Car;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import cn.sangyy.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;

    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car, HttpServletRequest request){
        try {
            Employee employee = LoginContext.getLoginInfo(request);
            if( car.getId()!=null){
                return carService.updateCar(car);
            }
            else
                carService.insert(car,employee);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 通过id获取对象信息
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            Car car = carService.selectById(id);
            return AjaxResult.me().setResultObj(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有
    * @return
    */
    @GetMapping("/list")
    public AjaxResult list(){
        try {
            List<Car> list = carService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/listPage")
    public AjaxResult listPageByQuery(@RequestBody CarQuery query) {
        PageList<Car> pageList = carService.getPageList(query);
        return AjaxResult.me().setResultObj(pageList);
    }
    /**
    * @author Sangyy
    * 批量上架
    */
    @PostMapping("/batchOnSale")
    public AjaxResult batchOnSale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginInfo = LoginContext.getLoginInfo(request);
        String message = carService.batchOnSale(ids,loginInfo);
        return AjaxResult.me().setMessage(message);
    }
    /**
     * @author Sangyy
     * 批量下架
     */
    @PostMapping("/batchOffSale")
    public AjaxResult batchOffSale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginInfo = LoginContext.getLoginInfo(request);
        carService.batchOffSale(ids,loginInfo);
        return AjaxResult.me();
    }
}
